package com.kingjw.lease.web.admin.schedule;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kingjw.lease.model.entity.LeaseAgreement;
import com.kingjw.lease.model.enums.LeaseStatus;
import com.kingjw.lease.web.admin.service.LeaseAgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 定时任务存放位置
 * @Component 表示将这个类注入容器中
 */
@Component
public class ScheduledTasks {

    @Autowired
    private LeaseAgreementService leaseAgreementService;

    /**
     *   ┌───────────── second (0-59)
     *   │ ┌───────────── minute (0 - 59)
     *   │ │ ┌───────────── hour (0 - 23)
     *   │ │ │ ┌───────────── day of the month (1 - 31)
     *   │ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC)
     *   │ │ │ │ │ ┌───────────── day of the week (0 - 7)
     *   │ │ │ │ │ │          (0 or 7 is Sunday, or MON-SUN)
     *   │ │ │ │ │ │
     *   * * * * * *
     */
    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseStatus() {

        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        Date now = new Date();
        updateWrapper.le(LeaseAgreement::getLeaseEndDate, now);
        updateWrapper.eq(LeaseAgreement::getStatus, LeaseStatus.SIGNED);
        // 只判断“已签约”和“退租待确认”的过期时间
        updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);

        leaseAgreementService.update(updateWrapper);
    }
}